package com.example.tellwin.mine.presenter;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.mine.contract.MineDetailContract;
import com.google.gson.JsonObject;

import java.io.File;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/12/1
 * @Describe ：
 **/
public class MineDetailPresenter extends RxPresenter<MineDetailContract.View> implements MineDetailContract.Presenter<MineDetailContract.View> {

    private TwjfApi mApi;

    @Inject
    public MineDetailPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    @Override
    public void getGradeList() {
        mApi.getGradeList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if(contentBean!=null&&contentBean.getGradeList()!=null){
                    mView.gradeListResult(contentBean.getGradeList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void getSubjectList() {
        mApi.getSubjectList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if(contentBean!=null&&contentBean.getSubjectList()!=null){
                    mView.subjectListResult(contentBean.getSubjectList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void getQualificationsList() {
        mApi.getQualificationsList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if(contentBean!=null&&contentBean.getQualificationsList()!=null){
                    mView.qualificationsListResult(contentBean.getQualificationsList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void getMandarinList() {
        mApi.getMandarinList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if(contentBean!=null&&contentBean.getMandarinList()!=null){
                    mView.mandarinListResult(contentBean.getMandarinList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void updateUserInfo(JsonObject jsonObject) {
        LogUtils.e(jsonObject.toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        mApi.updateUserInfo(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.updateUserInfoResult();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void uploadFile(String path) {
        File file = new File(path);
        String name = file.getName();

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", name, RequestBody.create(MediaType.parse("*/*"), file)) // 第一个参数传到服务器的字段名，第二个你自己的文件名，第三个MediaType.parse("*/*")和我们之前说的那个type其实是一样的
                .build();
        mApi.uploadFile(requestBody, new SimpleCallback<String>(mView) {
            @Override
            public void onSuccess(String s) {
                mView.uploadFileResult(s);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void applyAnswer(JsonObject jsonObject) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        mApi.applyAnswer(requestBody , new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.applyAnsweResult();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
